<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/1/10
 * Time: 9:25
 */
namespace app\admin\controller;

use think\Db;
use think\Request;
use Qiniu\Auth;// 引入鉴权类
use Qiniu\Storage\UploadManager;// 引入上传类
use Qiniu\Processing\PersistentFop;
require_once('../extend/qiniu/autoload.php');
class Poster extends Base {
    public function index(){
        if (request()->isAjax()) {
            extract(input());
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('poster')->alias('a')
                ->count();// 获取总条数
            $lists = Db::name('poster')->alias('a')
                ->page($NowPage, $limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    public function add(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            if(empty($params['video'])){
                return json(['code' => 100, 'msg' => '请上传视频']);
            }
            //转码的文件url
            $url = "http://xihaiqiniu.sooqee.cn/" . $params['video'];//未转码之前的视频url，用于获取时长
            //时长
            $a = file_get_contents($url . '?avinfo');
            $b = json_decode($a,true);
            $time = explode('.', $b['streams'][0]['duration']);
            $fen = floor($time[0]/60);
            $miao = floor(($time[0]-$fen*60)%60);
            $this->zhuanma($params['video']);
            $data = [
                'chirden_title' => $params['chirden_title'],//标题
                'chirden_url' => "http://xihaiqiniu.sooqee.cn/" . 'ndgl_' . $params['video'],//视频
                'chirden_time' => date('Y-m-d H:i:s'),//创建时间
                'catalogue_time' => $fen . ':' . $miao//视频时长
            ];
            $res = Db::table('up_poster')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    public function update(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            if(empty($params['video'])){
                return json(['code' => 100, 'msg' => '请上传视频']);
            }
            $old_url = Db::table('up_video_chirden')->where('chirden_id', $params['id'])->value('chirden_url');
            //转码的文件url
            //有视频但是和原视频地址不同
            if($params['video'] != '' && $params['video'] != $old_url){
                $url = "http://xihaiqiniu.sooqee.cn/" . $params['video'];//未转码之前的视频url，用于获取时长
                //时长
                $a = file_get_contents($url . '?avinfo');
                $b = json_decode($a,true);
                $time = explode('.', $b['streams'][0]['duration']);
                $fen = floor($time[0]/60);
                $miao = floor(($time[0]-$fen*60)%60);
                $this->zhuanma($params['video']);
                $data['chirden_url'] = "http://xihaiqiniu.sooqee.cn/" . 'ndgl_' . $params['video'];//视频地址
                $data['chirden_time'] = $fen . ':' . $miao;//视频时长
            }
            $data = [
                'chirden_title' => $params['chirden_title'],//标题
            ];
            $res = Db::table('up_poster')->where('chirden_id',$params['id'])->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_poster')->where('chirden_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$res);
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('poster')->where('chirden_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    /**
     * 转码
     * @param $key
     */
    public function zhuanma($key){
        set_time_limit (0);
        $accessKey = config('qiniu')['accessKey'];
        $secretKey = config('qiniu')['secretKey'];
        $bucket = config('qiniu')['bucket'];
        $auth = new Auth($accessKey, $secretKey);
        $pipeline = 'xihai';//多媒体队列名
        $force = false;
        //转码完成后通知到你的业务服务器。
//        $notifyUrl = '= 'http://cjtdcms.xuanwumobile.com/super#advert_manage';

        $config = new \Qiniu\Config();
        $pfop = new PersistentFop($auth, $config);
        $fops = "avthumb/mp4/vcodec/libx264/s/720x480|saveas/" . \Qiniu\base64_urlSafeEncode($bucket .  ":" . 'ndgl_' . $key);
        list($id, $err) = $pfop->execute($bucket, $key, $fops, $pipeline, '', $force);
        if($err !== null) {
            $res = $err;
        } else {
            $res = $id;
        }
    }
}